/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
 var constructMaximumBinaryTree = function(nums) {
    const buildTree = function(nums,left,right){
      if(left > right){ // 这里等于是可以的说明有一个节点元素
        return null
      }
      let maxIndex = -1
      let maxValue = -Infinity
      for(let i = left;i <= right;i++){
        if(maxValue < nums[i]){
          maxValue = nums[i]
          maxIndex = i
        }
      }
      let node = new TreeNode(maxValue)
      node.left = buildTree(nums,left,maxIndex-1)
      node.right = buildTree(nums,maxIndex+1,right)
      return node
    }
    return buildTree(nums,0,nums.length-1)
};